package myApp.KnorrAndroid.ultils;

import myApp.KnorrAndroid.common.KnorrInfo;
import android.content.Context;


public class StringUtil {

	public static String EMPTY_STRING = "";
    public static String SPACE_STRING = " ";
    public static char CHAR_SPLIT = '�';
    @SuppressWarnings("unused")
	private static UnicodeNormalizer unicodeNormalizer=UnicodeNormalizer.getInstance();
    public static String[] arrSpecialChar = {"‘", "’", "“", "�?", "„", "†", "‡","‰", "‹", "›", "♠", "♣", "♥", 
    										"♦", "‾", "�?", "↑", "→", "↓", "™", "!", "“", "#", "$", "%", "&", 
											"‘", "(", ")","*", "+", ",", "-", ".", "/",
											":", ";", "<", "=", ">", "?", "@", "[", "\\", "]", "_", "`",
											"{", "|", "}", "~", "–", "—", "¡", "¢", "£", "¤", "¥","¦",
											"§","¨", "©", "ª", "«", "¬", "¬", "®", "¯", "°", "±", "²", "³",
											"´", "µ","¶", "•", "¸", "¹", "º", "¿", "Ä", "Å", "Æ", "Ç", "Ë",
											"Î", "�?", "Ñ", "Ö", "×", "Ø", "Û", "Ü", "Þ", "ß", "ã", "ä", 
											"å", "æ", "ç", "ë", "î", "ï", "ð", "ñ", "õ","ö", "÷", "ø", "û", "ü", "þ", "ÿ"};
    /** The codau. */
    static char codau[] = {'à', 'á', 'ả', 'ã', 'ạ', 
    						'ă', 'ằ', 'ắ', 'ẳ', 'ẵ', 'ặ',
    						'â', 'ầ', 'ấ', 'ẩ', 'ẫ', 'ậ',
    						'À', 'Á', 'Ả', 'Ã', 'Ạ',
    						'Ă', 'Ằ', 'Ắ', 'Ẳ', 'Ẵ', 'Ặ',
    						'Â', 'Ầ', 'Ấ', 'Ẩ', 'Ẫ', 'Ậ',
    						'è', 'é', 'ẻ', 'ẽ', 'ẹ', 
    						'ê', 'ề', 'ế', 'ể', 'ễ', 'ệ',
    						'È', 'É', 'Ẻ', 'Ẽ', 'Ẹ',
    						'Ê', 'Ề', 'Ế', 'Ể', 'Ễ', 'Ệ',
    						'ì', 'í', 'ỉ', 'ĩ', 'ị',
    						'Ì', 'Í', 'Ỉ', 'Ĩ', 'Ị',
    						'ò', 'ó', 'ỏ', 'õ', 'ọ',
    						'ô', 'ồ', 'ố', 'ổ', 'ỗ', 'ộ',
    						'ơ', 'ờ', 'ớ', 'ở', 'ỡ', 'ợ',
    						'Ò', 'Ó', 'Ỏ', 'Õ', 'Ọ',
    						'Ô', 'Ồ', 'Ố', 'Ổ', 'Ỗ', 'Ộ',
    						'Ơ', 'Ờ', 'Ớ', 'Ở', 'Ỡ', 'Ợ',
    						'ù', 'ú', 'ủ', 'ũ', 'ụ',
    						'ư', 'ừ', 'ứ', 'ử', 'ữ', 'ự',
    						'Ù', 'Ú', 'Ủ', 'Ũ', 'Ụ',
    						'ỳ', 'ý', 'ỷ', 'ỹ', 'ỵ',
    						'Ỳ', 'Ý', 'Ỷ', 'Ỹ', 'Ỵ',
    						'đ',
    						'Đ'};
    static char khongdau[] = {'a', 'a', 'a', 'a', 'a',
		'a', 'a', 'a', 'a', 'a', 'a',
		'a', 'a', 'a', 'a', 'a', 'a',
		'A', 'A', 'A', 'A', 'A',
		'A', 'A', 'A', 'A', 'A', 'A',
		'A', 'A', 'A', 'A', 'A', 'A',
		'e', 'e', 'e', 'e', 'e', 
		'e', 'e', 'e', 'e', 'e', 'e',
		'E', 'E', 'E', 'E', 'E',
		'E', 'E', 'E', 'E', 'E', 'E',
		'i', 'i', 'i', 'i', 'i',
		'I', 'I', 'I', 'I', 'I',
		'o', 'o', 'o', 'o', 'o',
		'o', 'o', 'o', 'o', 'o', 'o',
		'o', 'o', 'o', 'o', 'o', 'o',
		'O', 'O', 'O', 'O', 'O',
		'O', 'O', 'O', 'O', 'O', 'O',
		'O', 'O', 'O', 'O', 'O', 'O',
		'u', 'u', 'u', 'u', 'u',
		'u', 'u', 'u', 'u', 'u', 'u',
		'U', 'U', 'U', 'U', 'U',
		'y', 'y', 'y', 'y', 'y',
		'Y', 'Y', 'Y', 'Y', 'Y',
		'd',
		'D'};
    
    static char charsInName[] = {'à', 'á', 'ả', 'ã', 'ạ',
    							'ă','ằ', 'ắ', 'ẳ', 'ẵ', 'ặ',
    							'â', 'ầ', 'ấ', 'ẩ', 'ẫ', 'ậ',
    							'À', 'Á', 'Ả', 'Ã', 'Ạ',
    							'Ă', 'Ằ', 'Ắ', 'Ẳ', 'Ẵ', 'Ặ',
    							'Â', 'Ầ', 'Ấ', 'Ẩ', 'Ẫ', 'Ậ',
    							'è', 'é', 'ẻ', 'ẽ', 'ẹ', 
    							'ê', 'ề', 'ế', 'ể', 'ễ', 'ệ',
    							'È', 'É', 'Ẻ', 'Ẽ', 'Ẹ',
    							'Ê', 'Ề', 'Ế', 'Ể', 'Ễ', 'Ệ',
    							'ì', 'í', 'ỉ', 'ĩ', 'ị',
    							'Ì', 'Í', 'Ỉ', 'Ĩ', 'Ị',
    							'ò', 'ó', 'ỏ', 'õ', 'ọ',
    							'ô', 'ồ', 'ố', 'ổ', 'ỗ', 'ộ',
    							'ơ', 'ờ', 'ớ', 'ở', 'ỡ', 'ợ',
    							'Ò', 'Ó', 'Ỏ', 'Õ', 'Ọ',
    							'Ô', 'Ồ', 'Ố', 'Ổ', 'Ỗ', 'Ộ',
    							'Ơ', 'Ờ', 'Ớ', 'Ở', 'Ỡ', 'Ợ',
    							'ù', 'ú', 'ủ', 'ũ', 'ụ',
    							'ư', 'ừ', 'ứ', 'ử', 'ữ', 'ự',
    							'Ù', 'Ú', 'Ủ', 'Ũ', 'Ụ',
    							'Ư', 'Ừ', 'Ứ', 'Ử', 'Ữ', 'Ự',
    							'ỳ', 'ý', 'ỷ', 'ỹ', 'ỵ',
    							'Ỳ', 'Ý', 'Ỷ', 'Ỹ', 'Ỵ',
    							'đ', 'Đ',
    							'a', 'A', 'b', 'B', 'c', 'C', 'd', 'D', 'e', 'E', 'f', 'F',
    							'g', 'G', 'h', 'H', 'i', 'I', 'j', 'J', 'k', 'K', 'l', 'L',
    							'm', 'M', 'n', 'N', 'o', 'O', 'p', 'P', 'q', 'Q', 'r', 'R', 's', 'S',
    							't', 'T', 'u', 'U', 'v', 'V', 'w', 'W', 'x', 'X', 'y', 'Y', 'z', 'Z', ' '};

    /**
    *  kiem tra ten co chua cac ki tu hop le
    *  @author: BangHN
    *  @param input
    *  @return
    *  @return: String
    *  @throws:
     */
    public static boolean isNameContainValidChars(String name) {
    	boolean isContain = false;
	    for (int j = 0, m = name.length(); j < m; j++) {
	    	isContain = false;
	    	for (int i = 0, n = charsInName.length; i < n; i++) {
	    		if (name.charAt(j) == charsInName[i]) {
	    			isContain = true;
	    			break;
	    		}
	    	}	    	
	    }
	    return isContain;
    }
    
    
    
    /**
    *  bo dau tieng viet
    *  @author: BangHN
    *  @param input
    *  @return
    *  @return: String
    *  @throws:
     */
    public static String getEngStringFromUnicodeString(String input) {
        if (isNullOrEmpty(input)) {
            return "";
        }
        input = input.trim();

        for (int i = 0; i < codau.length; i++) {
            input = input.replace(codau[i], khongdau[i]);
        }
        return input;
    }
    
    public static String replace(String _text, String _searchStr, String _replacementStr) {
// String buffer to store str
        StringBuffer sb = new StringBuffer();

// Search for search
        int searchStringPos = _text.indexOf(_searchStr);
        int startPos = 0;
        int searchStringLength = _searchStr.length();

// Iterate to add string
        while (searchStringPos != -1) {
            sb.append(_text.substring(startPos, searchStringPos)).append(_replacementStr);
            startPos = searchStringPos + searchStringLength;
            searchStringPos = _text.indexOf(_searchStr, startPos);
        }

// Create string
        sb.append(_text.substring(startPos, _text.length()));

        return sb.toString();
    }
    /**
     * check special char
     * @param str
     * @return
     * @Author : DoanDM
     * Since Dec 9, 2010
     */
    public static boolean checkSpecialChar(String str) {
        for (int i = 0; i < arrSpecialChar.length; i++) {
            if (str.indexOf(arrSpecialChar[i]) >= 0) {
                return true;
            }
        }
        return false;
    }
    
    /**
    *  Kiem tra nick name hop le
    *  @author: TruongHN3
    *  @param userName
    *  @return: boolean
    *  @throws:
     */
    public static boolean isValidateUserName(String userName) {
    	// UserName khong the toan so
    	// UserName chi chua cac ky tu a-z, A-Z, 0-9
    	Boolean isValid = false;
    	int length = userName.length();
        for (int i = 0; i < length; i++) {
        	char ch = userName.charAt(i);
        	if (((ch >= 0x30 && ch <= 0x39) || (ch >= 0x41 && ch <= 0x5a) || (ch >= 0x61 && ch <= 0x7a))) {
                isValid = true;
             }else{
            	 isValid = false;
            	 break;
             }
        }
        return isValid;
    }
    
    /**
     * validate phone
     * @param phone
     * @return
     * @Author : DoanDM
     * Since Dec 9, 2010
     */
    public static boolean isValidPhone(String phone) {
        int length = phone.length();
        for (int i = 0; i < length; i++) {
            if (phone.charAt(i) < '0' || phone.charAt(i) > '9') {
                return false;
            }
        }
        return true;
    }
    
    
    /**
	 * validate email
	 * @param strEmail
	 * @return
	 * @Author : DoanDM
	 * Since Dec 9, 2010
	 */
	public static boolean isValidateEmail(String email){
		if (email == null || email.length() == 0 || email.indexOf("@") == -1 || email.indexOf(" ") != -1) {
            return false;
        }
        int emailLenght = email.length();
        int atPosition = email.indexOf("@");

        String beforeAt = email.substring(0, atPosition);
        String afterAt = email.substring(atPosition + 1, emailLenght);

        if (beforeAt.length() == 0 || afterAt.length() == 0) {
            //#ifdef DEBUG
//#             //System.out.println("only @ is");
            //#endif
            return false;
        }

        //CHECK for .(dot) before @(at) = aaa.@domain.com
        if (email.charAt(atPosition - 1) == '.') {
            //#ifdef DEBUG
//#             //System.out.println(".(dot) before @(at)");
            //#endif
            return false;
        }

        //CHECK for .(dot) before @(at) = aaa@.domain.com
        if (email.charAt(atPosition + 1) == '.') {
            //#ifdef DEBUG
//#             //System.out.println("@.");
            //#endif
            return false;
        }

        //CHECK for .(dot) = email@domaincom
        if (afterAt.indexOf(".") == -1) {
            //#ifdef DEBUG
//#             //System.out.println("no dot(.)");
            //#endif
            return false;
        }

        //CHECK for ..(2 dots) = email@domain..com
        char dotCh = 0;
        for (int i = 0; i < afterAt.length(); i++) {
            char ch = afterAt.charAt(i);
            //soan validate
            if (!((ch >= 0x30 && ch <= 0x39) || (ch >= 0x41 && ch <= 0x5a) || (ch >= 0x61 && ch <= 0x7a) || (ch == 0x2e) || (ch == 0x2d) || (ch == 0x5f))) {
                //#ifdef DEBUG
//#                 //System.out.println("not valid chars");
                //#endif
                return false;
            }
            //end soan
            if ((ch == 0x2e) && (ch == dotCh)) {
                //#ifdef DEBUG
//#                 // System.out.println("find .. (2 dots) in @>");
                //#endif
                return false;
            }
            dotCh = ch;
        }

        //CHECK for double '@' = example@@domain.com
        if (afterAt.indexOf("@") != -1) {
            //#ifdef DEBUG
//#             //System.out.println("find 2 @");
            //#endif
            return false;
        }
        //check domain name 2-5 chars
        int ind = 0;
        do {
            int newInd = afterAt.indexOf(".", ind + 1);

            if (newInd == ind || newInd == -1) {
                String prefix = afterAt.substring(ind + 1);
                if (prefix.length() > 1 && prefix.length() < 6) {
                    break;
                } else {
                    //#ifdef DEBUG
//#                     //System.out.println("prefix not 2-5 chars");
                    //#endif
                    return false;
                }
            } else {
                ind = newInd;
            }
        } while (true);

        //CHECK for valid chars[a-z][A-Z][0-9][. - _]
        //CHECK for ..(2 dots)
        dotCh = 0;
        for (int i = 0; i < beforeAt.length(); i++) {
            char ch = beforeAt.charAt(i);
            if (!((ch >= 0x30 && ch <= 0x39) || (ch >= 0x41 && ch <= 0x5a) || (ch >= 0x61 && ch <= 0x7a) || (ch == 0x2e) || (ch == 0x2d) || (ch == 0x5f))) {
                //#ifdef DEBUG
//#                 //System.out.println("not valid chars");
                //#endif
                return false;
            }
            if ((ch == 0x2e) && (ch == dotCh)) {
                //#ifdef DEBUG
//#                 //System.out.println("find .. (2 dots)");
                //#endif
                return false;
            }
            dotCh = ch;
        }
        return true;
    }

	

    public static boolean isNullOrEmpty(String aString) {
        return (aString == null) || ("".equals(aString.trim()));
    }
    
    public static String getString(Context context, int id) {
    	return KnorrInfo.getInstance().getAppContext().getResources().getString(id);
    }
    public static String getString(int id) {
    	return KnorrInfo.getInstance().getAppContext().getResources().getString(id);
    }
    
    public static String[] getStringArray(Context context, int id) {
    	return context.getResources().getStringArray(id);
    }
    public static String[] getStringArray(int id) {
    	return KnorrInfo.getInstance().getAppContext().getResources().getStringArray(id);
    }

}
